<?php

require_once './include/common.inc.php';
//checkLogin();
$page_title = 'jans2en furniture - Products - Product Listing';
$menu_id = 5;

getMyParam('categoryid');
getMyParam('fid');
$fid = (int)$fid;

$cate_id = $fid>0 ? $fid : $categoryid;


if (!is_numeric($categoryid) || $categoryid <=0) {
    header("Location:products.php");
}

$new_item_ary = array(
'id' => '1001',
'name' => 'New Items',
'pic' => 'new_items.jpg',
'seq' => '1001',
'description' => getSystemValue('newitemset')
);

if ($categoryid>1000) {
    $sql = "SELECT `id`, `name`, `pic`, `pic_2`, `e_catalogue`, `seq`, `description` FROM `category` WHERE `fid` = 0 ORDER BY `seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[$rs['id']] = $rs;
    }

    // вƷid,ûвƷ
    $sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` GROUP BY c.`fid`";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $fid_ary[] = $rs['fid'];   //ͼƬ
        //$fid2_ary[] = $rs['category_id'];   //ͼƬ
    }
    
    // sqlfid=0ʱεһЩֻиĲƷֻʣһ
	// ˵вƷ`hidden_flag` = 0ĲƷ
	$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p, `product` pr WHERE pr.`id`=p.`product_id` AND c.`id`=p.`category_id` AND c.`fid` = 0 AND pr.`hidden_flag` = 0  GROUP BY p.`category_id`";
	//$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` AND c.`fid` = 0 GROUP BY p.`category_id`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$fid2_ary[] = $rs['category_id'];   //ͼƬ
	}

    
    foreach ($out_ary as $k=>$v) {
        if (!in_array($k, $fid_ary) && !in_array($k, $fid2_ary)) {
            unset($out_ary[$k]);
        }
    }

    $out_ary[] = $new_item_ary;

    $e_catalogue_name = 'New Items';

    if ($categoryid == 1001) {
        $categor_name = "New Items";
        $sql_where1 = '`new_flag`=1';
    }

    // *** page code start ***
    $page_url = 'product_list.php?categoryid=' . $categoryid . '&';
    //$sql = "SELECT `id` FROM `product` WHERE " . $sql_where1 . " AND `hidden_flag`=0";
    $sql = "SELECT p.`id` FROM `product` p, `product_category` pc, `category` c WHERE c.`id`=pc.`category_id` AND pc.`product_id`=p.`id` AND p.`new_flag`=1 AND p.`hidden_flag`=0 GROUP BY p.`id`";
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);

    $ppp = 12;
    getMyParam('page');
    $page = empty($page) || !ispage($page) ? 1 : $page;
    $start_limit = ($page - 1) * $ppp;
    if($start_limit > $sql_all_num) {
        $start_limit = 0;
        $page = 1;
    }

    $page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url);
    // *** page code end ***

    $photo_ary = array();
    //$sql = "SELECT `id`, `code`, `name`, `pic`, `small_pic`, `new_flag` FROM `product` WHERE " . $sql_where1 . " AND `hidden_flag`=0 ORDER BY `id` DESC LIMIT $start_limit, $ppp";
    $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag` FROM `product` p, `product_category` pc, `category` c WHERE c.`id`=pc.`category_id` AND pc.`product_id`=p.`id` AND p.`new_flag`=1 AND p.`hidden_flag`=0 GROUP BY p.`id` ORDER BY c.fid ASC, c.seq ASC, pc.`seq` DESC, p.`code` DESC LIMIT $start_limit, $ppp";
    //echo $sql;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $photo_ary[] = $rs;
        $js_id_ary[] = "'" . $rs['id'] . "'";
    }
} else {


    $sql = "SELECT `id`, `name`, `pic`, `pic_2`, `e_catalogue`, `seq`, `description` FROM `category` WHERE `fid` = 0 ORDER BY `seq` ASC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[$rs['id']] = $rs;
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];

            $e_catalogue_name = $rs['e_catalogue'];
        }
    }
    // вƷid,ûвƷ
    $sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` GROUP BY c.`fid`";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $fid_ary[] = $rs['fid'];   //ͼƬ
        //$fid2_ary[] = $rs['category_id'];   //ͼƬ
    }
    
    // sqlfid=0ʱεһЩֻиĲƷֻʣһ
	// ˵вƷ`hidden_flag` = 0ĲƷ
	$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p, `product` pr WHERE pr.`id`=p.`product_id` AND c.`id`=p.`category_id` AND c.`fid` = 0 AND pr.`hidden_flag` = 0  GROUP BY p.`category_id`";
	//$sql = "SELECT c.`fid`, p.`category_id` FROM `category` c, `product_category` p WHERE c.`id`=p.`category_id` AND c.`fid` = 0 GROUP BY p.`category_id`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$fid2_ary[] = $rs['category_id'];   //ͼƬ
	}

    foreach ($out_ary as $k=>$v) {
        if (!in_array($k, $fid_ary) && !in_array($k, $fid2_ary)) {
            unset($out_ary[$k]);
        }
    }

    if ($HAS_NEW_ITEM) {
        $out_ary[] = $new_item_ary;
    }

    $categoryid_str = $categoryid;
    $sql = "SELECT c.`id`, c.`name`, c.`pic`, c.`pic_2`, c.`seq`, c.`description` FROM `category` c, `product_category` pc WHERE c.id=pc.`category_id` AND c.`fid` = $cate_id GROUP BY c.`id` ORDER BY c.`seq` ASC";
    //echo $sql;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $sub_out_ary[] = $rs;
        $categoryid_str .= ',' . $rs['id'];
        if ($categoryid == $rs['id']) {
            $category_name = $rs['name'];
            $category_pic_2 = $rs['pic_2'];
        }
    }

    if ($fid>0) {
        $categoryid_str = $categoryid;
    }

    // *** page code start ***
    $page_url = 'product_list.php?fid=' . $fid . '&categoryid=' . $categoryid . '&';
    $sql = "SELECT c.`product_id` FROM `product` p, `product_category` c WHERE p.`id`=c.`product_id` AND c.`category_id` IN (" . $categoryid_str . ") AND c.`seq`>0 AND p.`hidden_flag`=0 GROUP BY p.`id`";
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);

    $ppp = 12;
    getMyParam('page');
    $page = empty($page) || !ispage($page) ? 1 : $page;
    $start_limit = ($page - 1) * $ppp;
    if($start_limit > $sql_all_num) {
        $start_limit = 0;
        $page = 1;
    }

    $page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url);
    // *** page code end ***

    $photo_ary = array();
    $sql = "SELECT p.`id`, p.`code`, p.`name`, p.`price_1`, p.`price_2`, p.`price_3`, p.`small_pic`, p.`pic`, p.`new_flag` FROM `product` p, `product_category` c, `category` ca WHERE ca.`id`=c.`category_id` AND p.`id`=c.`product_id` AND c.`category_id` IN (" . $categoryid_str . ") AND p.`hidden_flag`=0 GROUP BY p.`id` ORDER BY ca.seq ASC, c.seq DESC, p.`code` DESC, p.`id` DESC LIMIT $start_limit, $ppp";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $photo_ary[] = $rs;
        $js_id_ary[] = "'" . $rs['id'] . "'";
    }
}

//echo $sql;
//echo "<pre>";print_r($photo_ary);exit;
//$e_catalogue = $e_catalogue_name != '' ? '<a href="javascript:;" onclick="printall(' . $categoryid . ')"><img src="./images/button/jf_button_print-e-Catalogue.gif" alt="Printer Friendly" name="jf_button_printer_friendly" width="85" height="13" border="0" id="jf_button_printer_friendly"></a>&nbsp;' : "&nbsp;";
$e_catalogue = $fid==0 || empty($fid) ? '<a href="javascript:;" onclick="printall(' . $categoryid . ', 0)"><img src="./images/button/jf_button_print-e-Catalogue.gif" alt="Printer Friendly" name="jf_button_printer_friendly" width="85" height="13" border="0" id="jf_button_printer_friendly"></a>&nbsp;' : '<a href="javascript:;" onclick="printall(' . $fid . ', ' . $categoryid . ')"><img src="./images/button/jf_button_print-e-Catalogue.gif" alt="Printer Friendly" name="jf_button_printer_friendly" width="85" height="13" border="0" id="jf_button_printer_friendly"></a>&nbsp;';


$js_id_ary_str = !empty($js_id_ary) ? "new Array(" . implode(',', $js_id_ary) . ")" : "new Array()";

//echo $sql;
// ͼƬ
if (trim($category_pic_2) == "" || !file_exists("./attachments/" . $category_pic_2)) {
    $category_pic_2 = 'guide_product_default.jpg';
}

// add by apeng 2009.7.14
if($categoryid == '1001'){
	browseTracking(12,0,'Product > New Items'.'(page '.$page.')');
}else{
	if(isset($fid) && $fid != '0'){
    $sql = "SELECT a.`id`, a.`name` FROM `category` a WHERE a.id=".intval($fid)." ";
    $temp = $db->query($sql);
    while($rs = $db->fetch_array($temp)) {
			 $f_category_name = $rs['name'];
    }
		browseTracking(18,0,'Product > '.$f_category_name.' > '.$category_name.'(page '.$page.')');
	}else{
		browseTracking(18,0,'Product > '.$category_name.'(page '.$page.')');
	}	
}

require_once PrintEot('header');
require_once PrintEot('product_list');
require_once PrintEot('footer');

?>